# -*- coding: utf-8 -*-
#Importamos funciones standars
import datetime
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect

#Importamos funciones nuestras
from mundofacu.usuarios.models import carreras
from mundofacu.usuarios.decoradores import under_construction, staff_required, mod_required
from mundofacu.usuarios.funciones import save, exist
from formularios import Fnew, Fcarrera, Fcomentario
from models import noticias, comentarios


#Definimos nuestras vistas
def Vindex(request):
	T= "Portal de noticias"
	return render_to_response('news/index.html', {"title":T, }, context_instance=RequestContext(request))


def Vlistado(request):
	return render_to_response('lista.html', {"title":"Noticias:", "lista": noticias.objects.filter(fecha_publicacion__lte=datetime.date.today(),fecha_expiracion__gte=datetime.date.today()), }, context_instance=RequestContext(request))


def Vnewscarrera(request, tag):
	if exist(carreras,"id",tag):
		T= "Portal de noticias"
		S= "Carrera: " + carreras.objects.get(id=tag).nombre
		L= noticias.objects.filter(carrera=tag, fecha_publicacion__lte=datetime.date.today(),fecha_expiracion__gte=datetime.date.today()).all()
		return render_to_response('lista.html', {"title":T, "subject":S, "lista": L, }, context_instance=RequestContext(request))
	else: return redirect("/news")


@staff_required
def Vnewnew(request):
	T = "Creacion de Noticia"
	if request.method == 'POST':
		if Fnew(request.POST).is_valid():
			n = Fnew(request.POST).save(commit=False)
			n.autor = request.user
			save(n)
			return render_to_response('save.html', {"tipo":"Noticia creada", "exito":True,}, context_instance=RequestContext(request))
		else: return render_to_response('form.html', {'form':Fnew(request.POST),'title': T,}, context_instance=RequestContext(request))
	else: return render_to_response("form.html", {"form": Fnew(),}, context_instance=RequestContext(request))


@staff_required
def Vnewcarrera(request):
	T = "Creacion de Carrera"
	if request.method == 'POST':
		if Fcarrera(request.POST).is_valid():
			save(Fcarrera(request.POST))
			return render_to_response('save.html', {"tipo":"Carrera creada", "exito":True,}, context_instance=RequestContext(request))
		else: return render_to_response('form.html', {'form':Fcarrera(request.POST),'title': T,}, context_instance=RequestContext(request))
	else: return render_to_response("form.html", {"form": Fcarrera(),}, context_instance=RequestContext(request))


def Ver(request,id_new):
	if exist(noticias,"id",id_new):
		if request.method == 'POST':
			if Fcomentario(request.POST).is_valid():
				c = comentarios(texto=request.POST['texto'],autor=request.user,noticia=noticias.objects.get(id=id_new))
				c.save()
				return redirect(request.path)
		T = "Noticia: " + noticias.objects.get(id=id_new).titulo
		return render_to_response('news/ver.html', {"title":T,'noticia': noticias.objects.get(id=id_new), 'comentarios': comentarios.objects.filter(noticia=id_new), 'form': Fcomentario(), }, context_instance=RequestContext(request))
	else: return redirect('/usr')